<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="800px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="155px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label="名称" prop="fullName">
              <el-input
                v-trim
                v-model="dataForm.fullName"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="编码" prop="enCode">
              <el-input
                v-trim
                v-model="dataForm.enCode"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="WebHook类型" prop="webhookType">
              <el-select
                v-model="dataForm.webhookType"
                placeholder="请选择"
                :style="{ width: '100%' }"
                @change="handelWebhookTypeChange"
              >
                <el-option
                  v-for="(item, index) in webHookTypeOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="WebHook地址" prop="webhookAddress">
              <el-input
                v-trim
                v-model="dataForm.webhookAddress"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
                true
                type="textarea"
                :rows="1"
                :autosize="{ minRows: 1, maxRows: 4 }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="认证类型" prop="approveType">
              <el-select
                :disabled="dataForm.webhookType == '2'"
                v-model="dataForm.approveType"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
                @change="handelApproveTypeChange"
              >
                <el-option
                  v-for="(item, index) in approveTypeOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.id"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col
            :span="24"
            v-if="dataForm.webhookType == '1' && dataForm.approveType == '2'"
          >
            <el-form-item label="Bearer令牌" prop="bearer">
              <span slot="label">
                <el-tooltip content="密钥" placement="top">
                  <i
                    class="icon-ym icon-ym-header-about"
                    style="font-size: 14px"
                  ></i>
                </el-tooltip>
                Bearer令牌
              </span>
              <el-input
                v-trim
                v-model="dataForm.bearer"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="应用地址" prop="address">
              <el-input
                v-trim
                v-model="dataForm.address"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="排序" prop="sortCode">
              <el-input-number
                v-model="dataForm.sortCode"
                placeholder="数字文本"
                :step="1"
                controls-position="right"
              >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态" prop="enabledMark">
              <el-switch
                v-model="dataForm.enabledMark"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="说明" prop="description">
              <el-input
                v-model="dataForm.description"
                placeholder="请输入"
                :style="{ width: '100%' }"
                true
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4 }"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" @click="visible = false">
        取 消</el-button
      >
      <el-button :loading="btnLoading" type="primary" @click="dataFormSubmit()">
        确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { getInfo, Update, Create } from "@/views/message/api/mcAccountConfig";
export default {
  components: {},
  props: {
    category: {
      type: String,
      default: "8",
    },
  },
  data() {
    return {
      visible: false,
      loading: false,
      btnLoading: false,
      dataForm: {
        category: "",
        fullName: "",
        enCode: "",
        webhookType: "2",
        webhookAddress: "",
        approveType: "1",
        bearer: "",
        sortCode: 0,
        enabledMark: 1,
        description: "",
      },
      rules: {
        fullName: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        enCode: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
          {
            pattern: "^[A-Za-z0-9_]+$",
            message: "只能输入字母（大写和小写）、数字或下划线",
            trigger: "blur",
          },
        ],
        webhookType: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        webhookAddress: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        approveType: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        bearer: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
      },
      webHookTypeOptions: [],
      approveTypeOptions: [
        { fullName: "无需认证", id: "1" },
        { fullName: "bearer令牌", id: "2" },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getWebHookTypeOptions();
  },
  mounted() {},
  methods: {
    handelApproveTypeChange(val) {
      //无需认证
      if (val == "1") {
        // Bearer令牌
        this.dataForm.bearer = "";
      }
    },
    handelWebhookTypeChange(val) {
      //企业微信
      if (val == "2") {
        // 认证类型
        this.dataForm.approveType = "1";
        // Bearer令牌
        this.dataForm.bearer = "";
      }
    },
    getWebHookTypeOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "msgWebHookSendType",
        })
        .then((res) => {
          this.webHookTypeOptions = res;
        });
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          getInfo(this.dataForm.id).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
          this.dataForm.enabledMark = 1;
          this.dataForm.webhookType = "2"; // 默认WebHook类型 企业微信
          this.dataForm.approveType = "1"; // 默认认证类型 无需认证
          this.dataForm.category = this.category;
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          var _data = this.dataList();
          this.btnLoading = true;
          const formMethod = this.dataForm.id ? Update : Create;
          formMethod(_data)
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.btnLoading = false;
                  this.$emit("refresh", true);
                },
              });
            })
            .catch(() => {
              this.btnLoading = false;
            });
        }
      });
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.enabledMark = parseInt(_data.enabledMark);
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.enabledMark = parseInt(_dataAll.enabledMark);
      this.dataForm = _dataAll;
    },
  },
};
</script>
